package com.whstudio.task;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;

import com.whstudio.util.mvc.dao.AbstractDAO;
import com.whstudio.util.xml.IDocument;

/**
 * 任务管理数据操作类
 * 维护original_task、review_task表
 * @author wgp
 *
 */
public class TaskDAO extends AbstractDAO{
	/**
	 * 新增原始任务信息
	 * @param executeDate
	 * @param taskContent
	 * @return
	 * @throws SQLException
	 */
	public Integer insertOriginalTask(String taskName,Date executeDate,String taskContent)  throws SQLException{
		String sqlString="insert into original_task (task_name,execute_date,task_content) values(?,?,?)";
		Connection connection=getConnection();
		//写入original_task表
		PreparedStatement statement=connection.prepareStatement(sqlString);
		statement.setString(1, taskName);
		statement.setDate(2, executeDate);
		statement.setString(3, taskContent);
		statement.executeUpdate();
		ResultSet set=statement.getGeneratedKeys();
		Integer taskId=null;
		if(set.next()){
			taskId=set.getInt(1);
		}
		//结束操作，关闭连接
		endConnection(set, statement, connection);
		return taskId;
	}
	
	/**
	 * 更新原始任务信息
	 * @param taskId
	 * @param columnName
	 * @param valueString
	 * @return
	 * @throws SQLException
	 */
	public Integer updateOriginalTask(Integer taskId,String columnName,String valueString)  throws SQLException{
		return editTableColumn(taskId, "task_id", "original_task", columnName, valueString);
	}
	
	/**
	 * 删除原始任务信息
	 * @param taskId
	 * @return
	 * @throws SQLException
	 */
	public Integer deleteOriginalTask(Integer taskId)  throws SQLException{
		String sqlString="delete from original_task where task_id=?";
		Connection connection=getConnection();
		//写入original_task表
		PreparedStatement statement=connection.prepareStatement(sqlString);
		statement.setInt(1, taskId);
		Integer resultInteger=statement.executeUpdate();
		//结束操作，关闭连接
		endConnection(null, statement, connection);
		return resultInteger;
	}
	
	/**
	 * 删除所有原始任务信息
	 * @return
	 * @throws SQLException
	 */
	public Integer deleteAllOriginalTask()  throws SQLException{
		return deleteAllDataOfTable("original_task");
	}
	
	/**
	 * 获取原始任务数
	 * @param taskName
	 * @param executeDate
	 * @return
	 * @throws SQLException
	 */
	public Integer getOriginalTaskCount() throws SQLException{
		return getTableRowCount("original_task");
	}
	
	/**
	 * 删除所有复习任务信息
	 * @return
	 * @throws SQLException
	 */
	public Integer deleteAllReviewTask()  throws SQLException{
		return deleteAllDataOfTable("review_task");
	}
	
	/**
	 * 获取复习任务数
	 * @param taskName
	 * @param executeDate
	 * @return
	 * @throws SQLException
	 */
	public Integer getReviewTaskCount() throws SQLException{
		return getTableRowCount("review_task");
	}
	
	/**
	 * 写入review_task表
	 * @param taskIdInteger
	 * @param executeDate
	 * @return
	 * @throws SQLException
	 */
	public int[] insertIntoReviewTask(Integer taskIdInteger,Date executeDate) throws SQLException{
		String sqlString="insert into review_task (task_id,review_times,review_date) values(?,?,?)";
		Connection connection=getConnection();
		PreparedStatement statement=connection.prepareStatement(sqlString);
		
		Date[] reviews=new Date[6];
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(executeDate);
		
		calendar.add(Calendar.HOUR_OF_DAY, 1);
		reviews[0]=new Date(calendar.getTimeInMillis());
		calendar.add(Calendar.DATE, 1);
		reviews[1]=new Date(calendar.getTimeInMillis());
		calendar.add(Calendar.DATE, 3);
		reviews[2]=new Date(calendar.getTimeInMillis());
		calendar.add(Calendar.DATE, 6);
		reviews[3]=new Date(calendar.getTimeInMillis());
		calendar.add(Calendar.DATE, 13);
		reviews[4]=new Date(calendar.getTimeInMillis());
		calendar.add(Calendar.DATE, 27);
		reviews[5]=new Date(calendar.getTimeInMillis());
		
		for (int i=0;i<reviews.length;i++) {
			statement.setInt(1, taskIdInteger);
			statement.setInt(2, i+1);
			statement.setDate(3, reviews[i]);
			statement.addBatch();
		}
		
		int[] results=statement.executeBatch();
		
		endConnection(null, statement, connection);
		return results;
	}
	
	/**
	 * 根据任务编号查询任务名称
	 * @param taskId
	 * @return
	 * @throws SQLException
	 */
	public String queryTaskNameFromOriginalTask(Integer taskId) throws SQLException{
		return queryColumnFromTable(taskId, "task_id", "original_task", "task_name");
	}
	
	/**
	 * 根据任务编号查询记录
	 * @param taskId
	 * @return
	 * @throws SQLException
	 */
	public IDocument queryRowFromOriginalTask(Integer taskId) throws SQLException{
		return queryRowFromTable(taskId, "task_id", "original_task");
	}
	
	/**
	 * 查询当天原始任务
	 * @return
	 * @throws SQLException
	 */
	public IDocument queryTodayTasks() throws SQLException{
		String sqlString="select * from original_task where execute_date=CURRENT_DATE and if_valid=1";
		return getResultDocumentBySql(sqlString);
	}

	/**
	 * 查询当天复习任务
	 * @return
	 * @throws SQLException
	 */
	public IDocument queryTodayReviewTasks() throws SQLException{
		String sqlString="select * from original_task a,review_task b where a.task_id=b.task_id and b.review_date=CURRENT_DATE and if_valid=1";
		return getResultDocumentBySql(sqlString);
	}
	
	/**
	 * 查询所有原始任务
	 * @return
	 * @throws SQLException
	 */
	public IDocument queryAllOriginalTasks() throws SQLException{
		return queryAllRowFromTable("original_task");
	}
	
}
